package cn.listener;

import cn.TCSP;
import cn.util.RandomJugement;
import cn.util.TCUtil;
import cn.wetcland.base.WetclandWorldName;
import cn.wetcland.dao.GlobalMarketDao;
import cn.wetcland.dao.WetclandPlayerDao;
import cn.wetcland.item.SeniorItems;
import cn.wetcland.panel.GlobalMarketPanel;
import cn.wetcland.panel.SignInPanel;
import cn.wetcland.pojo.GlobalMarket;
import cn.wetcland.pojo.WetclandPlayer;
import com.mcrmb.PayApi;
import com.tripleying.qwq.LocaleLanguageAPI.LocaleLanguageAPI;
import me.yic.xconomy.XConomyAPI;
import net.minecraft.server.v1_16_R3.NBTTagCompound;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.craftbukkit.v1_16_R3.inventory.CraftItemStack;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta;

import java.math.BigDecimal;
import java.util.*;

public class InventoryListener implements Listener {

    @EventHandler
    public void inventoryClickEvent(InventoryClickEvent event) {
        Inventory inventory = event.getClickedInventory();
        HumanEntity whoClicked = event.getWhoClicked();
        InventoryView view = event.getView();
        int slot = event.getRawSlot();
        if (slot == -999) return;
        if (view.getTitle().equals("§b主菜单")) {
            event.setCancelled(true);
            Player player = (Player) whoClicked;
            switch (slot) {
                case 10:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getLandmarkMenu());
                    break;
                case 12:
                    player.chat("/tc open globalMarket");
                    break;
                case 14:
                    whoClicked.teleport(Bukkit.getWorld("world").getSpawnLocation());
                    break;
                case 16:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getMarryMenu());
                    break;
                case 19:
                    player.sendMessage("§c预留功能位");
                    break;
                case 21:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getServiceShoppingMenu());
                    break;
                case 23:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getShortcutToolMenu());
                    break;
                case 25:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(SignInPanel.getPlayerSignInPanel(player));
                    break;
                case 28:
                    player.chat("/jobs browse");
                    break;
            }
        } else if (view.getTitle().equals("§d传送点")) {
            event.setCancelled(true);
            switch (slot) {
                case 0:
                    whoClicked.teleport(TCUtil.getLocationByNameInYaml("mainHall"));
                    break;
                case 2:
                    whoClicked.teleport(Bukkit.getWorld(WetclandWorldName.PLOT_WORLD.getName()).getSpawnLocation());
                    break;
                case 4:
                    whoClicked.teleport(Bukkit.getWorld(WetclandWorldName.RES_WORLD.getName()).getSpawnLocation());
                    break;
                case 6:
                    whoClicked.teleport(Bukkit.getWorld(WetclandWorldName.WORLD_NETHER.getName()).getSpawnLocation());
                    break;
                case 8:
                    whoClicked.teleport(Bukkit.getWorld(WetclandWorldName.WORLD_THE_END.getName()).getSpawnLocation());
                    break;
                case 9:
                {
                    Location shopping = TCUtil.getLocationByNameInYaml("shopping");
                    if (shopping == null) {
                        whoClicked.sendMessage("§c不好意思，商店在整改");
                        return;
                    }
                    whoClicked.closeInventory();
                    whoClicked.teleport(shopping);
                    break;
                }
                case 26:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getMainMenu());
                    break;
            }
        } else if (view.getTitle().equals("§d便捷工具")) {
            event.setCancelled(true);
            String whoClickedName = whoClicked.getName();
            switch (slot) {
                case 0: {
                    TCUtil.saveLocationInYaml(whoClickedName + "_home1", whoClicked.getLocation());
                    whoClicked.sendMessage("§a成功设置家1坐标");
                    whoClicked.closeInventory();
                }
                break;
                case 1: {
                    Location locationByNameInYaml = TCUtil.getLocationByNameInYaml(whoClickedName + "_home1");
                    if (locationByNameInYaml == null) {
                        whoClicked.sendMessage("§c您好像没有设置过家1坐标");
                        return;
                    }
                    whoClicked.teleport(locationByNameInYaml);
                    whoClicked.closeInventory();
                }
                break;
                case 3: {
                    TCUtil.saveLocationInYaml(whoClickedName + "_home2", whoClicked.getLocation());
                    whoClicked.sendMessage("§a成功设置家2坐标");
                    whoClicked.closeInventory();
                }
                break;
                case 4: {
                    Location locationByNameInYaml = TCUtil.getLocationByNameInYaml(whoClickedName + "_home2");
                    if (locationByNameInYaml == null) {
                        whoClicked.sendMessage("§c您好像没有设置过家2坐标");
                        return;
                    }
                    whoClicked.teleport(locationByNameInYaml);
                    whoClicked.closeInventory();
                }
                break;
                case 6: {
                    TCUtil.saveLocationInYaml(whoClickedName + "_home3", whoClicked.getLocation());
                    whoClicked.sendMessage("§a成功设置家3坐标");
                    whoClicked.closeInventory();
                }
                break;
                case 7: {
                    Location locationByNameInYaml = TCUtil.getLocationByNameInYaml(whoClickedName + "_home3");
                    if (locationByNameInYaml == null) {
                        whoClicked.sendMessage("§c您好像没有设置过家3坐标");
                        return;
                    }
                    whoClicked.closeInventory();
                    whoClicked.teleport(locationByNameInYaml);
                }
                break;
                case 11: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() == 0) {
                        whoClicked.sendMessage("§c不好意思，该功能只限VIP/SVIP/至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/weather clear");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/weather clear");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b会员玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "天气修改为晴天");
                }
                break;
                case 13: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() == 1 || attrPlayer.getMemberType() == 0) {
                        whoClicked.sendMessage("§c不好意思，该功能只限SVIP/至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/weather rain");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/weather rain");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b会员玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "天气修改为雨天");
                }
                break;
                case 15: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() != 3) {
                        whoClicked.sendMessage("§c不好意思，该功能只限至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/weather thunder");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/weather thunder");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b至尊SVIP玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "天气修改为雷雨");
                }
                break;
                case 18: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() != 3) {
                        whoClicked.sendMessage("§c不好意思，该功能只限至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/time set 0");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/time set 0");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b至尊SVIP玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "时间修改为清晨");
                }
                break;
                case 20: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() != 3) {
                        whoClicked.sendMessage("§c不好意思，该功能只限至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/time set 1000");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/time set 1000");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b至尊SVIP玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "时间修改为中午");
                }
                break;
                case 22: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() != 3) {
                        whoClicked.sendMessage("§c不好意思，该功能只限至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/time set 12000");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/time set 12000");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b至尊SVIP玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "时间修改为日落黄昏");
                }
                break;
                case 24: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() != 3) {
                        whoClicked.sendMessage("§c不好意思，该功能只限至尊SVIP专属");
                        return;
                    }
                    if (whoClicked.isOp()) {
                        ((Player) whoClicked).chat("/time set 18000");
                        return;
                    }
                    whoClicked.setOp(true);
                    ((Player) whoClicked).chat("/time set 18000");
                    whoClicked.setOp(false);
                    whoClicked.closeInventory();
                    Bukkit.broadcastMessage("§b至尊SVIP玩家" + whoClickedName + "将" + TCUtil.getStringWorld(whoClicked.getWorld().getName()) + "时间修改为夜晚");
                }
                break;
                case 27: {
                    whoClicked.openInventory(Bukkit.createInventory(null, InventoryType.WORKBENCH, "§a便携工作台"));
                }
                break;
                case 29: {
                    ((Player) whoClicked).chat("/lay");
                    whoClicked.closeInventory();
                }
                break;
                case 31: {
                    whoClicked.closeInventory();
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() == 0 || attrPlayer.getMemberType() == 1) {
                        whoClicked.sendMessage("§c不好意思， 您不是SVIP/至尊SVIP会员");
                        return;
                    }
                    whoClicked.getWorld().strikeLightningEffect(whoClicked.getLocation());
                }
                break;
                case 33: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClickedName);
                    if (attrPlayer.getMemberType() == 0) {
                        whoClicked.sendMessage("§c不好意思， 您不是会员");
                        return;
                    }
                    ((Player) whoClicked).setAllowFlight(true);
                    ((Player) whoClicked).setFlying(true);
                    whoClicked.sendMessage("§a已为您开启飞行");
                    whoClicked.closeInventory();
                }
                break;
                case 36: {
                    ((Player) whoClicked).chat("/tc modifyTitle");
                    whoClicked.closeInventory();
                }
                break;
                case 38: {
                    ((Player) whoClicked).chat("/pvpc pvp off");
                    whoClicked.closeInventory();
                }
                break;
                case 39: {
                    ((Player) whoClicked).chat("/pvpc pvp on");
                    whoClicked.closeInventory();
                }
                break;
                case 53: {
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getMainMenu());
                }
                break;
            }
        } else if (view.getTitle().equals("§d求婚系统")) {
            event.setCancelled(true);
            switch (slot) {
                case 2:
                    XConomyAPI xConomyAPI = TCSP.getxConomyAPI();
                    double doubleValue = xConomyAPI.getbalance(xConomyAPI.translateUUID(whoClicked.getName())).doubleValue();
                    if (doubleValue < 10000) {
                        whoClicked.sendMessage("§c不好意思，您没有足够的金币");
                        return;
                    }
                    // 扣款
                    xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(10000), false);
                    whoClicked.getInventory().addItem(SeniorItems.proposalConch());
                    whoClicked.sendMessage("§a购买成功");
                    break;
                case 4:
                    ((Player)whoClicked).chat("/tc marry divorce");
                    whoClicked.closeInventory();
                    break;
                case 8:
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getMainMenu());
                    break;
            }
        } else if (view.getTitle().equals("§d服务商城")) {
            event.setCancelled(true);
            switch (slot) {
                case 2: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                    int memberType = attrPlayer.getMemberType();
                    if (memberType >= 1) {
                        whoClicked.sendMessage("§c不好意思，您不能降级或重复购买");
                        return;
                    }
                    int look = PayApi.look(whoClicked.getName());
                    if (look < 20) {
                        whoClicked.sendMessage("§c不好意思，您的节操不足20");
                        return;
                    }
                    PayApi.Pay(whoClicked.getName(), "20", "VIP/月费", true);
                    attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                    attrPlayer.setMemberType(1);
                    Calendar instance = Calendar.getInstance();
                    instance.add(Calendar.MONTH, 1);
                    attrPlayer.setMemberEndDate(instance.getTime());
                    SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
                    WetclandPlayerDao mapper = sqlSession.getMapper(WetclandPlayerDao.class);
                    mapper.updatePlayer(attrPlayer);
                    sqlSession.close();
                    Bukkit.broadcastMessage("§d————————————恭喜————————————");
                    Bukkit.broadcastMessage("§b玩家" + whoClicked.getName() + "成为本服 §eVIP会员");
                    Bukkit.broadcastMessage("§d————————————————————————————");
                    ((Player)whoClicked).playSound(whoClicked.getLocation(), "wetcland.bgm_funkyou", 5f, 1f);
                }
                break;
                case 4: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                    int memberType = attrPlayer.getMemberType();
                    if (memberType >= 2) {
                        whoClicked.sendMessage("§c不好意思，您不能降级或重复购买");
                        return;
                    }
                    int look = PayApi.look(whoClicked.getName());
                    if (look < 35) {
                        whoClicked.sendMessage("§c不好意思，您的节操不足35");
                        return;
                    }
                    PayApi.Pay(whoClicked.getName(), "35", "SVIP/月费", true);
                    attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                    attrPlayer.setMemberType(2);
                    Calendar instance = Calendar.getInstance();
                    instance.add(Calendar.MONTH, 2);
                    attrPlayer.setMemberEndDate(instance.getTime());
                    SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
                    WetclandPlayerDao mapper = sqlSession.getMapper(WetclandPlayerDao.class);
                    mapper.updatePlayer(attrPlayer);
                    sqlSession.close();
                    Bukkit.broadcastMessage("§d————————————恭喜————————————");
                    Bukkit.broadcastMessage("§b玩家" + whoClicked.getName() + "成为本服 §eSVIP会员");
                    Bukkit.broadcastMessage("§d————————————————————————————");
                    ((Player)whoClicked).playSound(whoClicked.getLocation(), "wetcland.bgm_funkyou", 5f, 1f);
                }
                break;
                case 6: {
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                    int memberType = attrPlayer.getMemberType();
                    if (memberType >= 3) {
                        whoClicked.sendMessage("§c不好意思，您不能降级或重复购买");
                        return;
                    }
                    int look = PayApi.look(whoClicked.getName());
                    if (look < 49) {
                        whoClicked.sendMessage("§c不好意思，您的节操不足49");
                        return;
                    }
                    PayApi.Pay(whoClicked.getName(), "49", "至尊SVIP/月费", true);
                    attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                    attrPlayer.setMemberType(3);
                    Calendar instance = Calendar.getInstance();
                    instance.add(Calendar.MONTH, 3);
                    attrPlayer.setMemberEndDate(instance.getTime());
                    SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
                    WetclandPlayerDao mapper = sqlSession.getMapper(WetclandPlayerDao.class);
                    mapper.updatePlayer(attrPlayer);
                    sqlSession.close();
                    Bukkit.broadcastMessage("§d————————————恭喜————————————");
                    Bukkit.broadcastMessage("§b玩家" + whoClicked.getName() + "成为本服 §e至尊SVIP会员");
                    Bukkit.broadcastMessage("§d————————————————————————————");
                    ((Player)whoClicked).playSound(whoClicked.getLocation(), "wetcland.bgm_funkyou", 5f, 1f);
                }
                break;
                case 26: {
                    whoClicked.closeInventory();
                    whoClicked.openInventory(TCSP.getMainMenu());
                }
                break;
            }
        } else if (view.getTitle().equals("§d全球市场导航")) {
            event.setCancelled(true);
            if (slot == 15) {
                whoClicked.closeInventory();
                whoClicked.openInventory(GlobalMarketPanel.getPublishPanel());
            } else if (slot == 13) {
                whoClicked.closeInventory();
                Inventory personalPanel = GlobalMarketPanel.getPersonalPanel();
                whoClicked.openInventory(personalPanel);
                SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
                GlobalMarketDao mapper = sqlSession.getMapper(GlobalMarketDao.class);
                List<GlobalMarket> byPlayerName = mapper.findByPlayerName(whoClicked.getName());
                for (GlobalMarket globalMarket : byPlayerName) {
                    NBTTagCompound nbtTagCompound = TCUtil.convertByteArrToNBT(globalMarket.getItemByte());
                    ItemStack itemStack = TCUtil.convertNBTTOItemStack(nbtTagCompound);
                    ItemMeta itemMeta = itemStack.getItemMeta();
                    List<String> lore = itemMeta.getLore();
                    lore.add("§d物品编号=" + globalMarket.getId());
                    itemMeta.setLore(lore);
                    itemStack.setItemMeta(itemMeta);
                    personalPanel.addItem(itemStack);
                }
                sqlSession.close();
            } else if (slot == 11) {
                whoClicked.closeInventory();
                LinkedList<Inventory> globalMarketInventories = GlobalMarketPanel.getGlobalMarketInventories();
                for (Inventory inventory1 : globalMarketInventories) {
                    System.out.println("==============");
                    System.out.println(inventory1.getSize());
                }
                //whoClicked.openInventory(globalMarketInventories.get(0));
                whoClicked.openInventory(globalMarketInventories.size() == 0 ? GlobalMarketPanel.getGlobalMarketPanel() : globalMarketInventories.getFirst());
            }
        } else if (view.getTitle().equals("§d全球市场发布")) {
            if (event.getRawSlot() <= 53)
                if (event.getRawSlot() != 13) event.setCancelled(true);
            if (slot == 27 || slot == 29 || slot == 31 || slot == 33) {
                ItemStack item = inventory.getItem(slot + 9);
                ItemMeta itemMeta = item.getItemMeta();
                List<String> lore = itemMeta.getLore();
                String s = lore.get(0).split("=")[1];
                int i = Integer.parseInt(s);
                lore.set(0, "§a当前数量=" + ++i);
                itemMeta.setLore(lore);
                item.setItemMeta(itemMeta);

                ItemStack item1 = inventory.getItem(44);
                ItemMeta itemMeta1 = item1.getItemMeta();
                List<String> lore1 = itemMeta1.getLore();
                String s1 = lore1.get(0).split("=")[1];
                int i1 = Integer.parseInt(s1);

                int r1 = Integer.parseInt(inventory.getItem(36).getItemMeta().getLore().get(0).split("=")[1]);
                int r2 = Integer.parseInt(inventory.getItem(38).getItemMeta().getLore().get(0).split("=")[1]);
                int r3 = Integer.parseInt(inventory.getItem(40).getItemMeta().getLore().get(0).split("=")[1]);
                int r4 = Integer.parseInt(inventory.getItem(42).getItemMeta().getLore().get(0).split("=")[1]);
                lore1.set(0, "§a当前总额=" + r1 * r2 * r3 * r4);
                itemMeta1.setLore(lore1);
                item1.setItemMeta(itemMeta1);
            }
            if (slot == 45 || slot == 47 || slot == 49 || slot == 51) {
                ItemStack item = inventory.getItem(slot - 9);
                ItemMeta itemMeta = item.getItemMeta();
                List<String> lore = itemMeta.getLore();
                String s = lore.get(0).split("=")[1];
                int i = Integer.parseInt(s);
                if (i == 1) return;
                lore.set(0, "§a当前数量=" + --i);
                itemMeta.setLore(lore);
                item.setItemMeta(itemMeta);

                ItemStack item1 = inventory.getItem(44);
                ItemMeta itemMeta1 = item1.getItemMeta();
                List<String> lore1 = itemMeta1.getLore();
                String s1 = lore1.get(0).split("=")[1];
                int i1 = Integer.parseInt(s1);
                int r1 = Integer.parseInt(inventory.getItem(36).getItemMeta().getLore().get(0).split("=")[1]);
                int r2 = Integer.parseInt(inventory.getItem(38).getItemMeta().getLore().get(0).split("=")[1]);
                int r3 = Integer.parseInt(inventory.getItem(40).getItemMeta().getLore().get(0).split("=")[1]);
                int r4 = Integer.parseInt(inventory.getItem(42).getItemMeta().getLore().get(0).split("=")[1]);
                lore1.set(0, "§a当前总额=" + r1 * r2 * r3 * r4);
                itemMeta1.setLore(lore1);
                item1.setItemMeta(itemMeta1);
            }
            if (event.getClick() == ClickType.SHIFT_LEFT && slot == 44) {
                WetclandPlayer wetclandPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                SqlSession openSession = TCSP.getSqlSessionFactory().openSession(true);
                GlobalMarketDao mapper2 = openSession.getMapper(GlobalMarketDao.class);
                List<GlobalMarket> byPlayerName = mapper2.findByPlayerName(whoClicked.getName());
                switch (wetclandPlayer.getMemberType()) {
                    case 0: {
                        if (byPlayerName.size() > 9) {
                            whoClicked.closeInventory();
                            ((Player) whoClicked).sendTitle("§a不好意思，发布数量已达上限, 充值为会员可解锁更多", "", 10, 10, 10);
                            openSession.close();
                            return;
                        }
                    }
                    break;
                    case 1: {
                        if (byPlayerName.size() > 12) {
                            whoClicked.closeInventory();
                            ((Player) whoClicked).sendTitle("§a不好意思，发布数量已达上限, 充值为SVIP会员可解锁更多", "", 10, 10, 10);
                            openSession.close();
                            return;
                        }
                    }
                    break;
                    case 2: {
                        if (byPlayerName.size() > 15) {
                            whoClicked.closeInventory();
                            ((Player) whoClicked).sendTitle("§a不好意思，发布数量已达上限, 充值为至尊SVIP会员可解锁更多", "", 10, 10, 10);
                            openSession.close();
                            return;
                        }
                    }
                    break;
                    case 3: {
                        if (byPlayerName.size() > 18) {
                            whoClicked.closeInventory();
                            ((Player) whoClicked).sendTitle("§a不好意思，发布数量已达上限", "", 10, 10, 10);
                            openSession.close();
                            return;
                        }
                    }
                    break;
                }
                openSession.close();
                ItemStack item = inventory.getItem(13);
                if (item == null) {
                    whoClicked.sendMessage("§c请在中心圈放入物品");
                    return;
                }
                // 总金额
                double res = Double.parseDouble(inventory.getItem(44).getItemMeta().getLore().get(0).split("=")[1]);
                ItemMeta itemMeta = item.getItemMeta();
                List<String> lore = itemMeta.getLore();
                if (lore == null) lore = new ArrayList<>();
                lore.add("§c价格=" + res);
                lore.add("§b物品所属者=" + whoClicked.getName());
                System.out.println(whoClicked.getName());
                itemMeta.setLore(lore);
                item.setItemMeta(itemMeta);
                // 物品 NBT 字节
                byte[] bytes = TCUtil.convertNBTToByteArr(item);
                // 手续费
                double tips = res * 0.05;

                WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                XConomyAPI xConomyAPI = TCSP.getxConomyAPI();
                // 查余额
                BigDecimal getbalance = xConomyAPI.getbalance(xConomyAPI.translateUUID(whoClicked.getName()));
                long l = getbalance.longValue();
                if (l < tips) {
                    ((Player) whoClicked).sendTitle("§c您的金币不足", "§a手续费: " + tips, 10, 10, 10);
                    whoClicked.closeInventory();
                    return;
                }
                attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() - tips);
                // 扣款
                xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(tips), false);
                // 数据库存入 物品信息
                SqlSessionFactory sqlSessionFactory = TCSP.getSqlSessionFactory();
                SqlSession sqlSession = sqlSessionFactory.openSession(true);
                try {
                    GlobalMarketDao mapper = sqlSession.getMapper(GlobalMarketDao.class);
                    GlobalMarket globalMarket = new GlobalMarket();
                    globalMarket.setItemByte(bytes);
                    globalMarket.setOwnerName(whoClicked.getName());
                    globalMarket.setOwnerId(attrPlayer.getId());
                    //globalMarket.setItemName(Util.getItemStackName(item));
                    if (item.getItemMeta().getDisplayName() != null && !item.getItemMeta().getDisplayName().equals("")) {
                        globalMarket.setItemName(item.getItemMeta().getDisplayName());
                    } else {
                        String itemName = LocaleLanguageAPI.getItemName(item);
                        if (itemName == null || itemName.equals("")) {
                            globalMarket.setItemName(item.getType().name());
                        } else {
                            globalMarket.setItemName(itemName);
                        }
                    }
                    globalMarket.setPrice(res);
                    Calendar.Builder builder = new Calendar.Builder();
                    Calendar build = builder.build();
                    build.setTime(new Date());
                    globalMarket.setStartTime(build.getTime());
                    build.add(Calendar.DATE, 7);
                    globalMarket.setEndTime(build.getTime());
                    globalMarket.setNbt(CraftItemStack.asNMSCopy(item).save(new NBTTagCompound()).toString());
                    WetclandPlayerDao mapper1 = sqlSession.getMapper(WetclandPlayerDao.class);
                    mapper1.updatePlayer(attrPlayer);
                    mapper.insertOne(globalMarket);
                    ((Player) whoClicked).sendTitle("§a发布成功", "", 10, 10, 10);
                    inventory.setItem(13, null);
                } catch (Exception e) {
                    ((Player) whoClicked).sendTitle("§c发布发生异常", "", 10, 10, 10);
                    e.printStackTrace();
                } finally {
                    whoClicked.closeInventory();
                    sqlSession.close();
                }
            }
        } else if (view.getTitle().equals("§b全球市场-我已发布")) {
            event.setCancelled(true);
            if (event.getCurrentItem() != null & slot > 26 & slot <= 44 & event.getClick() == ClickType.SHIFT_LEFT) {
                ItemStack currentItem = event.getCurrentItem();
                ItemStack clone = currentItem.clone();
                ItemMeta itemMeta = clone.getItemMeta();
                List<String> lore1 = itemMeta.getLore();
                String s = lore1.get(lore1.size() - 1).split("=")[1];

                SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
                GlobalMarketDao mapper = sqlSession.getMapper(GlobalMarketDao.class);
                try {
                    GlobalMarket byId = mapper.findById(Integer.parseInt(s));
                    Date nowDate = new Date();
                    // 过期
                    if (byId.getEndTime().before(nowDate)) {
                        lore1.remove(lore1.size() - 1);
                        lore1.remove(lore1.size() - 1);
                        lore1.remove(lore1.size() - 1);
                        itemMeta.setLore(lore1);
                        clone.setItemMeta(itemMeta);
                        whoClicked.getInventory().addItem(clone);
                        inventory.clear(slot);
                        mapper.deleteById(Integer.parseInt(s));
                    } else {
                        long l = byId.getEndTime().getTime() - nowDate.getTime();
                        whoClicked.sendMessage("§c该物品还未到下架时间");
                        whoClicked.sendMessage("§a剩余 " + l + " MS");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    sqlSession.close();
                }
            }
        } else if (view.getTitle().equals("§b全球市场")) {
            event.setCancelled(true);
            ItemStack item = inventory.getItem(49);
            int currentPage = Integer.parseInt(item.getItemMeta().getDisplayName().split("/")[0].substring(2)) - 1;
            int totalPage = Integer.parseInt(item.getItemMeta().getDisplayName().split("/")[1]) - 1;
            if (slot == 47) {
                if (currentPage == 0) {
                    whoClicked.sendMessage("§c没有上一页了");
                    return;
                }
                whoClicked.closeInventory();
                whoClicked.openInventory(GlobalMarketPanel.getGlobalMarketInventories().get(currentPage - 1));
            } else if (slot == 51) {
                if (currentPage == totalPage) {
                    whoClicked.sendMessage("§c没有下一页了");
                    return;
                }
                whoClicked.closeInventory();
                whoClicked.openInventory(GlobalMarketPanel.getGlobalMarketInventories().get(currentPage + 1));
            } else if (slot <= 35 && event.getClick() == ClickType.SHIFT_LEFT && event.getCurrentItem() != null) {
                ItemStack currentItem = event.getCurrentItem();
                if (currentItem.getType() == Material.BARRIER) {
                    whoClicked.sendMessage("§c不好意思, 该物品已售出");
                    return;
                }
                if (currentItem.getItemMeta().getLore().get(currentItem.getItemMeta().getLore().size() - 2).split("=")[1].equals(whoClicked.getName())) {
                    whoClicked.sendMessage("§c您不能买自己发布的物品");
                    return;
                }
                ItemStack clone = currentItem.clone();
                ItemMeta itemMeta = clone.getItemMeta();
                List<String> lore = itemMeta.getLore();
                // 出售价格
                double res = Double.parseDouble(lore.get(lore.size() - 3).split("=")[1]);

                XConomyAPI xConomyAPI = TCSP.getxConomyAPI();
                // 查余额
                BigDecimal getbalance = xConomyAPI.getbalance(xConomyAPI.translateUUID(whoClicked.getName()));
                long l = getbalance.longValue();
                if (l < res) {
                    ((Player) whoClicked).sendTitle("§c您的金币不足", "", 10, 10, 10);
                    whoClicked.closeInventory();
                    return;
                }
                SqlSession sqlSession = TCSP.getSqlSessionFactory().openSession(true);
                GlobalMarketDao mapper = sqlSession.getMapper(GlobalMarketDao.class);

                WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() - res);
                // 扣款
                xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(res), false);

                mapper.deleteById(Integer.parseInt(lore.get(lore.size() - 1).split("=")[1]));
                sqlSession.close();
                lore.remove(lore.size() - 1);
                lore.remove(lore.size() - 1);
                lore.remove(lore.size() - 1);
                itemMeta.setLore(lore);
                clone.setItemMeta(itemMeta);
                whoClicked.getInventory().addItem(clone);
                // 设置成屏障
                currentItem.setType(Material.BARRIER);
            }
        } else if (view.getTitle().equals("§a签到系统")) {
            event.setCancelled(true);
            ItemStack item = inventory.getItem(slot);
            if (item != null) {
                Calendar instance = Calendar.getInstance();
                int day = instance.get(Calendar.DAY_OF_MONTH);
                int month = instance.get(Calendar.MONTH) + 1;
                int year = instance.get(Calendar.YEAR);
                int monthDay = TCUtil.getMonthDay(month);
                ItemMeta itemMeta = item.getItemMeta();
                String displayName = itemMeta.getDisplayName();
                if (displayName.endsWith("未签到") && slot + 1 == day) {
                    int num = TCUtil.signInAndSaveYaml(whoClicked.getName());
                    whoClicked.sendMessage("§a今日签到成功");
                    whoClicked.sendMessage("§a当前最大连续天数为" + num + ", 请坚持每天签到哦~");
                    itemMeta.setDisplayName("§a" + year + "年" + month + "月" + day + "日已签到");
                    item.setType(Material.SUNFLOWER);
                    item.setItemMeta(itemMeta);
                    // 必须要是玩家背包
                    PlayerInventory playerInventory = whoClicked.getInventory();
                    // 固定签到得 1钻石 3面包 2牛肉 2猪肉
                    playerInventory.addItem(
                            new ItemStack(Material.DIAMOND, 1),
                            new ItemStack(Material.COOKED_BEEF, 2),
                            new ItemStack(Material.COOKED_PORKCHOP, 2),
                            new ItemStack(Material.BREAD, 3)
                    );
                    WetclandPlayer attrPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                    // 固定签到得 100金币
                    attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 100);
                    XConomyAPI xConomyAPI = TCSP.getxConomyAPI();
                    xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(100), true);
                    // 处理 首月 和 满月
                    if (day == 1) {
                        attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 1000);
                        xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(1000), true);
                        PayApi.Manual(whoClicked.getName(), 1, "5", "首月签到");
                        attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                        Bukkit.broadcastMessage("§d┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍");
                        Bukkit.broadcastMessage("§e恭喜玩家完成首月签到, 获得额外1000金币和5节操币");
                        Bukkit.broadcastMessage("§d┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍ ┑(￣▽ ￣)┍");
                        TCUtil.updateWetclandPlayerInDB(attrPlayer);
                        return;
                    } else if (num == monthDay) {
                        attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 30000);
                        xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(30000), true);
                        PayApi.Manual(whoClicked.getName(), 1, "20", "满月签到");
                        attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                        Bukkit.broadcastMessage("§d(～o▔▽▔)～o o～(▔▽▔o～) (～o▔▽▔)～o o～(▔▽▔o～) (～o▔▽▔)～o o～(▔▽▔o～)");
                        Bukkit.broadcastMessage("§e恭喜玩家完成满月签到, 获得额外30000金币和20节操币");
                        Bukkit.broadcastMessage("§d◕‿◕ ◕‿◕ ◕‿◕ ◕‿◕ ◕‿◕ ◕‿◕ ◕‿◕ ◕‿◕ ◕‿◕");
                        TCUtil.updateWetclandPlayerInDB(attrPlayer);
                        return;
                    }
                    // 处理连续签到天数
                    switch (num) {
                        case 3:
                            attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 2000);
                            xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(2000), true);
                            PayApi.Manual(whoClicked.getName(), 1, "1", "连续3天签到");
                            attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            Bukkit.broadcastMessage("§e恭喜玩家完成3天连续签到, 获得额外2000金币和1节操币");
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            TCUtil.updateWetclandPlayerInDB(attrPlayer);
                            break;
                        case 7:
                            attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 4000);
                            xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(4000), true);
                            PayApi.Manual(whoClicked.getName(), 1, "3", "连续7天签到");
                            attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            Bukkit.broadcastMessage("§e恭喜玩家完成7天连续签到, 获得额外4000金币和3节操币");
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            TCUtil.updateWetclandPlayerInDB(attrPlayer);
                            break;
                        case 10:
                            attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 6000);
                            xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(6000), true);
                            PayApi.Manual(whoClicked.getName(), 1, "5", "连续10天签到");
                            attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            Bukkit.broadcastMessage("§e恭喜玩家完成10天连续签到, 获得额外6000金币和5节操币");
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            TCUtil.updateWetclandPlayerInDB(attrPlayer);
                            break;
                        case 15:
                            attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 8000);
                            xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(8000), true);
                            PayApi.Manual(whoClicked.getName(), 1, "7", "连续15天签到");
                            attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            Bukkit.broadcastMessage("§e恭喜玩家完成15天连续签到, 获得额外8000金币和7节操币");
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            TCUtil.updateWetclandPlayerInDB(attrPlayer);
                            break;
                        case 20:
                            attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 10000);
                            xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(10000), true);
                            PayApi.Manual(whoClicked.getName(), 1, "9", "连续20天签到");
                            attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            Bukkit.broadcastMessage("§e恭喜玩家完成20天连续签到, 获得额外10000金币和9节操币");
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            TCUtil.updateWetclandPlayerInDB(attrPlayer);
                            break;
                        case 25:
                            attrPlayer.setGoldCoin(attrPlayer.getGoldCoin() + 15000);
                            xConomyAPI.changebalance(xConomyAPI.translateUUID(whoClicked.getName()), whoClicked.getName(), BigDecimal.valueOf(15000), true);
                            PayApi.Manual(whoClicked.getName(), 1, "12", "连续25天签到");
                            attrPlayer.setIntegrity(PayApi.look(whoClicked.getName()));
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            Bukkit.broadcastMessage("§e恭喜玩家完成25天连续签到, 获得额外15000金币和12节操币");
                            Bukkit.broadcastMessage("§d(*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*) (*▔＾▔*)");
                            TCUtil.updateWetclandPlayerInDB(attrPlayer);
                            break;
                    }
                } else if (displayName.endsWith("待签到")) {
                    whoClicked.sendMessage("§d不好意思，还没到签到时间");
                } else if (displayName.endsWith("已签到")) {
                    whoClicked.sendMessage("§d你已经签到了哦");
                }
            }
        } else if (view.getTitle().equals("§d强化面板")) {
            if (slot >= 27 || slot == 10 || slot == 12) {

            } else {
                event.setCancelled(true);
            }
            ItemStack item = inventory.getItem(10);
            ItemStack intensifier = inventory.getItem(12);
            if (slot != 15) return;
            if (item == null || intensifier == null) {
                whoClicked.sendMessage("§c请按照规定放置");
            } else {
                Material material = item.getType();
                //TODO 暂时 不限制 强化物品类型
                if (true/*material == Material.STONE_SWORD || material == Material.DIAMOND_SWORD ||
                        material == Material.GOLDEN_SWORD || material == Material.IRON_SWORD ||
                        material == Material.NETHERITE_SWORD || material == Material.WOODEN_SWORD*/) {
                    if (intensifier.getAmount() != 1) {
                        whoClicked.sendMessage("§c一次只能放一个强化石");
                        return;
                    }
                    if (item.getAmount() != 1) {
                        whoClicked.sendMessage("§c一次只能放一个强化物品");
                        return;
                    }
                    ItemMeta itemMeta = intensifier.getItemMeta();
                    String displayName = itemMeta.getDisplayName();
                    if (displayName.endsWith("强化石") && displayName.startsWith("§")) {
                        int itemLevel = TCUtil.getItemIntensifierLevel(item);
                        int itemIntensifierLevel = TCUtil.getItemIntensifierLevel(intensifier);
                        System.out.println(itemLevel + " -> " + itemIntensifierLevel);
                        if (itemIntensifierLevel == 1) {
                            if (RandomJugement.juge(0.7)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 1);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers())
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 1, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 1) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 2) {
                            if (RandomJugement.juge(0.75)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 3);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers()) {
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                }
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 3, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 3) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 3) {
                            if (RandomJugement.juge(0.8)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 4);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers()) {
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                }
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 4, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 4) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 4) {
                            if (RandomJugement.juge(0.85)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 5);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers()) {
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                }
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 5, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 5) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 5) {
                            if (RandomJugement.juge(0.9)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 6);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers()) {
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                }
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 6, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 6) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 6) {
                            if (RandomJugement.juge(0.94)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 7);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers()) {
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                }
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 7, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 7) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 7) {
                            if (RandomJugement.juge(0.96)) {
                                TCUtil.setItemIntensifierLevel(item, itemLevel + 8);
                                ItemMeta itemMeta1 = item.getItemMeta();
                                if (itemMeta1.hasAttributeModifiers()) {
                                    itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                                }
                                itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                        new AttributeModifier("damage", itemLevel + 8, AttributeModifier.Operation.ADD_NUMBER));
                                item.setItemMeta(itemMeta1);
                                whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 8) + "级");
                            } else {
                                inventory.clear(10);
                                whoClicked.closeInventory();
                                whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                            }
                        } else if (itemIntensifierLevel == 8) {
                            TCUtil.setItemIntensifierLevel(item, itemLevel + 10);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE,
                                    new AttributeModifier("damage", itemLevel + 10, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将武器强化到" + (itemLevel + 10) + "级");
                        }
                        // 清空强化石
                        inventory.clear(12);
                    } else {
                        whoClicked.sendMessage("§c你确定这是强化石吗?");
                    }
                } else {
                    whoClicked.sendMessage("§c抱歉，强化暂时不支持该类武器");
                }
            }
        } else if (view.getTitle().equals("§d淬炼面板")) {
            if (slot >= 27 || slot == 10 || slot == 12) {

            } else {
                event.setCancelled(true);
            }
            ItemStack item = inventory.getItem(10);
            ItemStack intensifier = inventory.getItem(12);
            if (slot != 15) return;
            if (item == null || intensifier == null) {
                whoClicked.sendMessage("§c请按照规定放置");
            } else {
                if (intensifier.getAmount() != 1) {
                    whoClicked.sendMessage("§c一次只能放一个淬炼石");
                    return;
                }
                if (item.getAmount() != 1) {
                    whoClicked.sendMessage("§c一次只能放一个淬炼物品");
                    return;
                }
                ItemMeta itemMeta = intensifier.getItemMeta();
                String displayName = itemMeta.getDisplayName();
                if (displayName.endsWith("淬炼石") && displayName.startsWith("§")) {
                    int itemLevel = TCUtil.getItemRefinerLevel(item);
                    int itemRefinerLevel = TCUtil.getItemRefinerLevel(intensifier);
                    System.out.println(itemLevel + " -> " + itemRefinerLevel);
                    if (itemRefinerLevel == 1) {
                        if (RandomJugement.juge(0.7)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 1);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("defense", itemLevel + 1, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 1, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 1) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的装备化成了青烟");
                        }
                    } else if (itemRefinerLevel == 2) {
                        if (RandomJugement.juge(0.75)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 3);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("damage", itemLevel + 3, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 3, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 3) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                        }
                    } else if (itemRefinerLevel == 3) {
                        if (RandomJugement.juge(0.8)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 4);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("damage", itemLevel + 4, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 4, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 4) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                        }
                    } else if (itemRefinerLevel == 4) {
                        if (RandomJugement.juge(0.85)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 5);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("damage", itemLevel + 5, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 5, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 5) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                        }
                    } else if (itemRefinerLevel == 5) {
                        if (RandomJugement.juge(0.9)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 6);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("damage", itemLevel + 6, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 6, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);

                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 6) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                        }
                    } else if (itemRefinerLevel == 6) {
                        if (RandomJugement.juge(0.94)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 7);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("damage", itemLevel + 7, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 7, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 7) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                        }
                    } else if (itemRefinerLevel == 7) {
                        if (RandomJugement.juge(0.96)) {
                            TCUtil.setItemRefinerLevel(item, itemLevel + 8);
                            ItemMeta itemMeta1 = item.getItemMeta();
                            if (itemMeta1.hasAttributeModifiers()) {
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                                itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                            }
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                    new AttributeModifier("damage", itemLevel + 8, AttributeModifier.Operation.ADD_NUMBER));
                            itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                    new AttributeModifier("toughness", itemLevel + 8, AttributeModifier.Operation.ADD_NUMBER));
                            item.setItemMeta(itemMeta1);
                            whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 8) + "级");
                        } else {
                            inventory.clear(10);
                            whoClicked.closeInventory();
                            whoClicked.sendMessage("§c炸裂，你的武器化成了青烟");
                        }
                    } else if (itemRefinerLevel == 8) {
                        TCUtil.setItemRefinerLevel(item, itemLevel + 10);
                        ItemMeta itemMeta1 = item.getItemMeta();
                        if (itemMeta1.hasAttributeModifiers()) {
                            itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR);
                            itemMeta1.removeAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS);
                        }
                        itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR,
                                new AttributeModifier("damage", itemLevel + 10, AttributeModifier.Operation.ADD_NUMBER));
                        itemMeta1.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS,
                                new AttributeModifier("toughness", itemLevel + 10, AttributeModifier.Operation.ADD_NUMBER));
                        item.setItemMeta(itemMeta1);
                        whoClicked.sendMessage("§a恭喜你，将装备淬炼到" + (itemLevel + 10) + "级");
                    }
                    // 清空淬炼石
                    inventory.clear(12);
                } else {
                    whoClicked.sendMessage("§c你确定这是淬炼石吗?");
                }
            }
        } else if (view.getTitle().equals("§d等级面板")) {
            if (slot != 11 && slot < 27) {
                event.setCancelled(true);
            }
            if (slot == 15) {
                ItemStack item = inventory.getItem(11);
                if (item == null) whoClicked.sendMessage("§c请放入等级石及其相应数量");
                else {
                    String displayName = item.getItemMeta().getDisplayName();
                    // 物品数量
                    int amount = item.getAmount();
                    WetclandPlayer wetclandPlayer = TCUtil.getAttrPlayer(whoClicked.getName());
                    // 当前玩家等级
                    int level = wetclandPlayer.getLevel();
                    int flag = -1;
                    switch (displayName) {
                        case "§d一级等级石":
                            if (level == 1 && amount == 3) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            } else if (level == 2 && amount == 6) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            } else if (level == 3 && amount == 10) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d二级等级石":
                            if (level == 4 && amount == 4) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d三级等级石":
                            if (level == 5 && amount == 8) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d四级等级石":
                            if (level == 6 && amount == 8) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d五级等级石":
                            if (level == 7 && amount == 8) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d六级等级石":
                            if (level == 8 && amount == 8) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d七级等级石":
                            if (level == 9 && amount == 8) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d八级等级石":
                            if (level == 10 && amount == 4) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            } else if (level == 11 && amount == 8) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d九级等级石":
                            if (level == 12 && amount == 4) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        case "§d十级等级石":
                            if (level == 13 && amount == 1) {
                                wetclandPlayer.setLevel(++level);
                                flag = 1;
                            }
                            break;
                        default:
                            whoClicked.sendMessage("§c你确定你放的是等级石吗?");
                            break;
                    }
                    // 标志位为 1 表示匹配成功
                    if (flag == 1) {
                        Map<String, Object> map = (Map<String, Object>) TCSP.getPlayerAttr().get(whoClicked.getName());
                        map.put("info", wetclandPlayer);
                        // 更新数据库
                        SqlSessionFactory sqlSessionFactory = TCSP.getSqlSessionFactory();
                        SqlSession sqlSession = sqlSessionFactory.openSession(true);
                        WetclandPlayerDao mapper = sqlSession.getMapper(WetclandPlayerDao.class);
                        mapper.updatePlayer(wetclandPlayer);
                        sqlSession.close();
                        inventory.clear(11);
                        whoClicked.closeInventory();
                        Bukkit.broadcastMessage("§a================");
                        Bukkit.broadcastMessage("§b恭喜玩家 " + whoClicked.getName() + "");
                        Bukkit.broadcastMessage("§d等级达到 " + level + "级，晋升为 §b" + TCUtil.getStringLevel(level));
                        Bukkit.broadcastMessage("§a================");
                    } else {
                        whoClicked.sendMessage("§c请放入等级石及其相应数量");
                    }
                }
            }
        }
    }

    @EventHandler
    public void inventoryDragEvent(InventoryDragEvent event) {
        System.out.println(event.getView().getTitle());
    }

    @EventHandler
    public void inventoryCloseEvent(InventoryCloseEvent event) {
        InventoryView view = event.getView();
        String title = view.getTitle();
        Inventory inventory = event.getInventory();
        HumanEntity player = event.getPlayer();
        if (title.equals("§d全球市场发布")) {
            if (inventory.getItem(13) != null) {
                ItemStack item = inventory.getItem(13);
                player.getInventory().addItem(item);
            }
        } else if (title.equals("§d强化面板") || title.equals("§d淬炼面板")) {
            if (inventory.getItem(10) != null) {
                player.getInventory().addItem(inventory.getItem(10));
            }
            if (inventory.getItem(12) != null) {
                player.getInventory().addItem(inventory.getItem(12));
            }
        } else if (title.equals("§d等级面板")) {
            if (inventory.getItem(11) != null) {
                player.getInventory().addItem(inventory.getItem(11));
            }
        } else if (title.equals("§a便携工作台")) {
            ItemStack[] contents = inventory.getContents();
            for (ItemStack content : contents) {
                if (content != null) player.getInventory().addItem(content);
            }
        }
    }

    @EventHandler
    public void PrepareAnvilEvent(PrepareAnvilEvent event) {
        AnvilInventory anvilInventory = event.getInventory();
        ItemStack item = anvilInventory.getItem(0);
        List<HumanEntity> viewers = event.getViewers();
        if (item != null) {
            ItemMeta itemMeta = item.getItemMeta();
            assert itemMeta != null;
            String name = itemMeta.getDisplayName();
            if (name.equals("§d[Wetcland]§b小助手") || name.equals("§d求婚海螺") ||
                    (name.startsWith("§d") && name.endsWith("等级石")) ||
                    (name.startsWith("§d") && name.endsWith("淬炼石")) ||
                    (name.startsWith("§d") && name.endsWith("强化石"))) {
                for (HumanEntity humanEntity : viewers) {
                    if (humanEntity != null) {
                        humanEntity.sendMessage("§c无法对该物品进行操作");
                    }
                }

                ItemStack[] contents = anvilInventory.getContents();
                for (ItemStack itemStack : contents) {
                    if (itemStack != null)
                        anvilInventory.getLocation().getWorld().dropItemNaturally(anvilInventory.getLocation(), itemStack);
                }
                anvilInventory.clear();
            }
        }
    }

}
